var V = new Vue({
    el: '#Vmain',
    data: () => ({
        tijiao: true,
        isallsel: false,
        Left: {},
        listdata: {
            content: [],
            footer: {}
        },//货柜产品
        falsedata:{},//未入仓产品
        dayin: {
            langname: 'zh',
            format: '1',
            selectdata: [],
            sort: '2',
            separate: '0',
            iconSize: '2',
            fontSize: '2',
        },
        warehouse: [],
        langs: [],
        batch: [],
        allche: [],
        levitedata: [],//入仓数据
        setcanceldata:[],//取消入仓的数据
        isexitone: true,
        combined: {//合计
            pronum: 0,//产品个数
            boxnum: 0,//箱数
            msnum: 0,//体积
            weight: 0,//重量
            maony: 0,//金额
        },
        /* 弹框数据 */
        combineddata: {
            //选择入仓的合计
            pronum: 0,//产品个数
            boxnum: 0,//箱数
            msnum: 0,//体积
            weight: 0,//重量
        },
        selWarehouseVal: 0,//仓库
        location: [],//对应仓库下的位置列表
        locationval: 0,//位置
    }),
    methods: {
        formatUnit: formatUnit,
        //根据选择的仓库获取对应的位置
        selWarehouse(){
            this.locationval = 0
            $.ajax({
                url: '/admin/logistics/containerdetail?wplace=&warehouse_id=' + this.selWarehouseVal,
                type: 'post',
                success: data => {
                    this.location = JSON.parse(data);
                }
            })
        },
        //入仓保存点击
        saveFun(){
            this.levitedata = []
            this.batch.forEach(element => {
                V.listdata.forEach((item) => {
                    item.buy_po_pack.forEach(el => {
                        if (el.id == element) {
                            this.levitedata.push({
                                po_id:item.id,//入仓的合同id
                                po_pack_id: el.id,//采购单id
                                pro_id: el.pro_id,//产品id
                                prochar_id: el.prochar_id,//产品角色id
                                qty: el.sum_qty,//货柜产品数量
                                price: el.price,//采购价格
                                ctn_m3: el.ctn_m3,//产品箱子体积
                                ctn_qty: el.ctn_qty,//装箱数量
                                is_item: el.is_item,//判断是否是配件
                                cost: V.Left.cost,//货柜费用
                                sum_cbm: V.Left.sum_cbm,//货柜体积
                                place_id: this.locationval,//位置id
                                warehouse_id: this.selWarehouseVal,//仓库id
                            })
                        }
                    });
                })
            });
            $.ajax({
                url: '/admin/logistics/containerarrival',
                type: 'post',
                data: {
                    prolist: this.levitedata,//产品信息
                    container_id: this.Left.id,
                    setstrong: ''
                },
                success: data => {
                    data = JSON.parse(data)
                    if (data.status != 1) {
                        change_mes_type('.modal-body .alert', data.status)
                        $('.modal-body .alert').show()
                        $('.modal-body .alert p').html('*' + data.msg + '<br>')
                    } else {
                        change_mes_type('#headeralert', data.status)
                        $('#headeralert').show()
                        $('#headeralert p').html('*' + data.msg + '<br>')
                        this.tijiao = false
                        $('#arrival_Modal').modal('toggle');
                        location.reload()
                    }
                }
            })
        },
        //计算入仓时的产品合计
        combinedata(){
            this.combineddata.pronum = 0
            this.combineddata.boxnum = 0
            this.combineddata.msnum = 0
            this.combineddata.weight = 0
            V.listdata.forEach((item)=>{
                item.buy_po_pack.forEach(el=>{
                    if(this.batch.indexOf(el.id)!=-1){
                        //表示找到了
                        this.combineddata.pronum = this.batch.length
                        this.combineddata.boxnum += el.qty
                        this.combineddata.msnum += el.sum_m3
                        this.combineddata.weight += el.sum_kgs
                    }
                })
            })
        },
        //进入时，默认数据获取
        getdata(){
            $('.loding').show();
            // this.isallsel=false;
            $.ajax({
                url: '/admin/logistics/containerdetail',
                type: 'post',
                data:{
                    id:this.GetQueryString('id'),
                    cdetail:'',
                    // issale_qty:this.isexitwo
                },
                success: data => {
                    data = JSON.parse(data)
                    this.warehouse = data.warehouse
                    this.Left = data.left
                    this.langs = data.langs
                    this.listdata = data.trueright;//全部的
                    this.falsedata = data.falsedata;//没有入仓的
                    this.listdata.forEach((item) => {
                        item.check = false
                        this.combined.pronum += item.buy_po_pack.length//产品个数
                        this.combined.boxnum += item.CTN
                        this.combined.msnum += item.CBM
                        this.combined.weight += item.KGS
                        this.combined.maony += item.CNY
                        item.buy_po_pack.forEach(element => {
                            element.check = false
                            this.allche.push(element.id)
                        });
                    })
                    // if(code==1){
                    //     this.isallsel=true;
                    //     this.selall();
                    // }else if(code==2) {
                    //     this.isallsel=false;
                    //     this.selall();
                    // }

                    $('.loding').hide()
                }
            })
        },
        //获取地址参数
        GetQueryString(name){
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null)return unescape(r[2]);
            return null;
        },
        //开航，到港，相关点击
        apifun(type, n, id, name) {
            $.ajax({
                url: '/admin/logistics/' + type,
                data: {
                    num: n,
                    id,
                    name
                },
                type: 'post',
                success: data => {
                    data = JSON.parse(data)
                    change_mes_type('#headeralert', data.status)
                    $('#headeralert').show()
                    $('#headeralert p').html(data.msg)
                    if (data.status == 1) {
                        this.getdata()
                    }

                }
            })
        },
        //全部勾选
        selall(){
            this.allche=[];
            this.listdata.forEach((item) => {
                item.check = this.isallsel
                item.buy_po_pack.forEach(el => {
                    el.check = this.isallsel;
                    this.allche.push(el.id)
                });
            })
            if (this.isallsel) {
                this.batch = this.allche
            } else {
                this.batch = []
            }
        },
        //合同勾选
        selallgroup(index){
            this.listdata[index].buy_po_pack.forEach(item => {
                item.check = this.listdata[index].check
                if (this.batch.includes(item.id)) {
                    if (!this.listdata[index].check)
                        this.batch.splice(this.batch.indexOf(item.id), 1)
                } else {
                    if (this.listdata[index].check)
                        this.batch.push(item.id)
                }
            })
            if (
                this.listdata.every(item => item.check)
            ) {
                this.isallsel = true
            } else {
                this.isallsel = false
            }
            this.$forceUpdate()
        },
        //产品勾选
        selone(index, key, id){
            if (this.batch.includes(id)) {
                this.batch.splice(this.batch.indexOf(id), 1)
            } else {
                this.batch.push(id)
            }
            if (
                this.listdata[index].buy_po_pack.every(item => item.check)
            ) {
                this.listdata[index].check = true
            } else {
                this.listdata[index].check = false
                this.isallsel = false
            }
            if (
                this.listdata.every(item => item.check)
            ) {
                this.isallsel = true
            } else {
                this.isallsel = false
            }
        },
        //取消入仓 合同id 采购清单id 产品id 产品角色id
        warehousechange(po_id,pack_id, pro_id, prochar_id){
            this.setcanceldata=[];
            V.listdata.forEach(item=>{
                if(item.id==po_id){
                    //获取提交的数据
                    item.buy_po_pack.forEach(el=>{
                        if(el.id==pack_id){
                            this.setcanceldata.push({
                                po_id:po_id,//入仓的合同id
                                po_pack_id: pack_id,//采购单id
                                pro_id: pro_id,//产品id
                                prochar_id: prochar_id,//产品角色id
                                qty: el.sum_qty,//货柜的数量
                                price: el.price,//采购价格
                                ctn_m3: el.ctn_m3,//产品箱子体积
                                ctn_qty: el.ctn_qty,//装箱数量
                                is_item: el.is_item,//判断是否是配件
                                cost: V.Left.cost,//货柜费用
                                sum_cbm: V.Left.sum_cbm,//货柜体积
                            })
                        }
                    })
                }
            })
            $.ajax({
                url: '/admin/logistics/containerarrival',
                data: {
                    setcancel: '',
                    setcanceldata:this.setcanceldata,
                    container_id: this.Left.id
                },
                type: 'post',
                success: data => {
                    data = JSON.parse(data)
                    change_mes_type('#headeralert', data.status)
                    $('#headeralert').show()
                    $('#headeralert p').html('*' + data.msg + '<br>')
                    if(data.status==1){
                        location.reload()
                    }
                }
            })
        },
        //显示货柜资料
        oneexit(){
            this.isexitone=!this.isexitone
        },
        //选择还没入仓产品
        twoexit(){
            this.listdata.forEach((element,listKey)=>{
                this.falsedata.forEach((item,index)=>{
                    if(element.id==item.id){
                        item.buy_po_pack.forEach((val,key)=>{
                            this.listdata[listKey].buy_po_pack[key].check=! this.listdata[listKey].buy_po_pack[key].check;
                            this.selone(listKey,key,val.id)
                        })
                    }
                })
            })
        },
        //打印点击
        dayindata(){
            this.dayin.container_id = this.GetQueryString('id')
            $.ajax({
                url: '/admin/logistics/preview',
                data: {
                    setddatas:this.dayin,
                    setdata:'',
                },
                type: 'post',
                success: data => {
                    location.href = '/admin/Logistics/preview?onetwothree';
                }
            })
        },
        //入仓点击
        openRUcang(){
            if(this.batch.length>0){
                $('#arrival_Modal').modal('show')
            }else {
                change_mes_type('#headeralert', 2)
                $('#headeralert').show()
                $('#headeralert p').html('*请勾选产品<br>')
            }
        }
    },
    mounted(){
        this.getdata()
        $("#arrival_Modal").on("hidden.bs.modal", () => {
            //重新设置仓库id，位置id
            this.selWarehouseVal = 0;
            this.locationval=0;
            $('.modal-body .alert').hide()
            $('.modal-body .alert p').html()
            $(this).removeData("bs.modal");
        });
    },
    watch:{
        //监听勾选的按钮
        batch() {
            this.combinedata();
        }
    }
})

